package com.samplecompany.gui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;

import com.samplecompany.gui.MashupPanel.MashupListView;


public class MashupForm extends Form<Void> implements MashupContainer {

  private static final long serialVersionUID = 1L;

  private final List<Component> children = new ArrayList<Component>();
  
  private LinkedList<Component> previouslyRenderedChildren;
  
  private final MashupListView mashupListView;
  
  /**
   * @param id
   */
  public MashupForm(String id) {
    super(id);
    super.add(mashupListView = new MashupListView("form-listview-id", children));
  }

  /**
   */
  @Override
  protected void onBeforeRender() {
    if (isChanged()) {
      mashupListView.removeAll();
    }
    
    previouslyRenderedChildren = new LinkedList<Component>(children);
    
    super.onBeforeRender();
  }
  
  /**
   * @return is changed
   */
  public boolean isChanged() {
    if (previouslyRenderedChildren != null) {
      return !((children.size() == previouslyRenderedChildren.size()) && previouslyRenderedChildren.containsAll(children));
    }
    
    return false;
  }
  
  /**
   */
  public boolean add(Component component) {
    return children.add(component);
  }

  /**
   */
  public void add(int index, Component element) {
    children.add(index, element);
  }

  /**
   */
  public boolean addAll(Collection<? extends Component> collection) {
    return children.addAll(collection);
  }

  /**
   */
  public boolean addAll(int index, Collection<? extends Component> collection) {
    return children.addAll(index, collection);
  }

  /**
   */
  public void clear() {
    children.clear();
  }

  /**
   */
  public boolean contains(Object o) {
    return children.contains(o);
  }

  /**
   */
  public boolean containsAll(Collection<?> collection) {
    return children.containsAll(collection);
  }

  /**
   */
  public int indexOf(Object o) {
    return children.indexOf(o);
  }

  /**
   */
  public boolean isEmpty() {
    return children.isEmpty();
  }

  /**
   */
  public int lastIndexOf(Object o) {
    return children.lastIndexOf(o);
  }

  /**
   */
  public boolean remove(Object o) {
    return children.remove(o);
  }

  /**
   */
  public Component remove(int index) {
    return children.remove(index);
  }

  /**
   */
  public boolean removeAll(Collection<?> collection) {
    return children.removeAll(collection);
  }

  /**
   */
  public boolean retainAll(Collection<?> collection) {
    return children.retainAll(collection);
  }

  /**
   */
  public Component set(int index, Component element) {
    if (children.size() <= index) {
      for (int subIndex = children.size(); subIndex <= index; subIndex++) {
        if (subIndex != index) {
          add(subIndex, new WebMarkupContainer(GID));
        } else {
          add(subIndex, element);
        }
      }
      
      return null;
    }
    
    return children.set(index, element);
  }

  /**
   */
  public List<Component> subList(int fromIndex, int toIndex) {
    return children.subList(fromIndex, toIndex);
  }

  /**
   */
  public Object[] toArray() {
    return children.toArray();
  }

  /**
   */
  public <T> T[] toArray(T[] a) {
    return children.toArray(a);
  }

}
